package org.csu.gp_store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.gp_store.bo.ProductBO;
import org.csu.gp_store.entity.Company;
import org.csu.gp_store.entity.Product;
import org.csu.gp_store.mapper.CompanyMapper;
import org.csu.gp_store.mapper.ProductMapper;
import org.csu.gp_store.service.ItemService;
import org.csu.gp_store.service.ProductService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ItemService itemService;

    @Override
    public List<ProductBO> getProductBOListByCategoryID(Long categoryID) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("categoryID", categoryID);
        List<Product> productList = productMapper.selectList(queryWrapper);
        QueryWrapper<Product> ProductQueryWrapper = new QueryWrapper<>();
        List<ProductBO> productBOList = new ArrayList<>();
        for (Product product : productList) {
            ProductQueryWrapper.eq("productID", product.getProductID());
            productBOList.add(ProductBO.toProductBO(
                    companyMapper.selectById(product.getCompanyID()),
                    product,itemService.getItemListByProductID(product.getProductID())));
        }
        return productBOList;

    }

    @Override
    public List<ProductBO> getProductBOList() {
        List<Product> ProductList = productMapper.selectList(null);
        List<ProductBO> productBOList = new ArrayList<>();
        for (Product product : ProductList) {
            Company company = companyMapper.selectById(product.getCompanyID());
            productBOList.add(ProductBO.toProductBO(company,product,itemService.getItemListByProductID(product.getProductID())));
        }
        return productBOList;
    }

//    @Override
//    public List<ProductBO> getProductListByProductID(Long  productID) {
//        Product product = productMapper.selectById(productID);
//        List<ProductBO> productBOList = new ArrayList<>();
//        Company company = new Company();
//        company = companyMapper.selectById(company.getCompanyID());
//        productBOList.add(ProductBO.toProductBO(company,product ));
//        return productBOList;
//    }

    @Override
    public List<ProductBO> getProductListByProductName(String ProductName) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_name", ProductName);
        List<Product> productList = productMapper.selectList(queryWrapper);
        List<ProductBO> productBOList = new ArrayList<>();
        Company company;
        for (Product product : productList) {
            company = companyMapper.selectById(product.getCompanyID());
            productBOList.add(ProductBO.toProductBO(company,product,itemService.getItemListByProductID(product.getProductID())));
        }
        return productBOList;
    }

    @Override
    public List<ProductBO> getProductListLikeProductName(String keyword) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("product_name", keyword);
        List<Product> productList = productMapper.selectList(queryWrapper);
        List<ProductBO> productBOList = new ArrayList<>();
        Company company;
        for (Product product : productList) {
            company = companyMapper.selectById(product.getCompanyID());
            productBOList.add(ProductBO.toProductBO( company,product,itemService.getItemListByProductID(product.getProductID()) ));

        }
        return productBOList;
    }

    @Override
    public ProductBO getProductByProductID(Long productID) {
        Product product = productMapper.selectById(productID);
        Company company = companyMapper.selectById(product.getCompanyID());
        return ProductBO.toProductBO( company,product, itemService.getItemListByProductID(product.getProductID()));
    }

    @Override
    public int updateProductStatus(Long productID, String status) {
        UpdateWrapper<Product> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("productID", productID).set("status", status);
        return productMapper.update(null, updateWrapper);
    }

//    @Override
//    public void updateProductPrice(Long productID, BigDecimal price) {
//        UpdateWrapper<Product> updateWrapper = new UpdateWrapper<>();
//        updateWrapper.eq("productID", productID).set("price", price);
//        productMapper.update(null, updateWrapper);
//    }

    @Override
    public int updateProductDescription(Long productID, String description) {
        UpdateWrapper<Product> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ProductID", productID).set("description", description);
        return productMapper.update(null, updateWrapper);
    }

    @Override
    public int updateProductName(Long productID, String productName) {
        UpdateWrapper<Product> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("productID", productID).set("product_name", productName);
        return productMapper.update(null, updateWrapper);
    }


    public int insertProduct(Product product) {
        return productMapper.insert(product);
    }

    @Override
    public int deleteProduct(Long productID) {
       return productMapper.deleteById(productID);
    }

    @Override
    public Long getProductID(@NotNull Long companyID,@NotNull String productName){
        LambdaQueryWrapper<Product> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Product::getCompanyID,companyID);
        lambdaQueryWrapper.eq(Product::getProductName,productName);
        List<Product> productList = productMapper.selectList(lambdaQueryWrapper);
        if(productList.size()>0){
            return productList.get(0).getProductID();
        }
        return null;
    }
}
